package com.yanq.liancar.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.adapter.FindMsgAdapter;
import com.yanq.liancar.base.BaseLYFragment;
import com.yanq.liancar.bean.FindMsgBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.net.HttpUtil;
import com.yanq.liancar.net.util.ProgressSubscriber;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.Constant;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;

public class CommentFragment extends BaseLYFragment {


    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;
    private int page = 1;
    private FindMsgAdapter findMsgAdapter;
    private String findType;
    private List<FindMsgBean.DataBean.ListBean> mListBeans=new ArrayList<>();
    private boolean isClear=true;
    private boolean isLastPage;
    public static CommentFragment newInstance(String findType) {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.FIND_TYPE, findType);
        CommentFragment fragment = new CommentFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.myfind);
        findType = getArguments().getString(Constant.FIND_TYPE);
        //init();
        initRecyclerview();

        initListener();

        initData();
    }


    private void initRecyclerview() {
        recyclerview.setNestedScrollingEnabled(false);
        recyclerview.setLayoutManager(new LinearLayoutManager(mActivity));

        findMsgAdapter = new FindMsgAdapter(mActivity, mListBeans);
        findMsgAdapter.setType(findType);
        recyclerview.setAdapter(findMsgAdapter);

    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                isClear = true;
                initData();
                refreshlayout.finishRefresh();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                isClear = false;
                if (!isLastPage){
                    initData();
                }else {
                    RxToast.normal("暂无更多数据");
                }
                refreshLayout.finishLoadMore();
            }

        });
    }

    private void initData(){
        OkGo.<String>post(Config.s + "llc/api/selectFindSysMassage")
                .tag(this)
                .params("token", UserBean.getcToken(mActivity))
                .params("type", findType)
                .params("pageNum",page)
                .params("pageSize", 10)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        FindMsgBean bean = gson.fromJson(response.body(), FindMsgBean.class);
                        if (bean.isIsSuccess()) {
                            if (bean.getCode()==200){
                                if (isClear){
                                    mListBeans.clear();
                                }
                                mListBeans.addAll(bean.getData().getList());
                                findMsgAdapter.notifyDataSetChanged();
                                isLastPage=bean.getData().isIsLastPage();
                            }else {
                               RxToast.info(bean.getOutputMessage());
                            }
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }

                    }
                });
    }
//    public void init() {
//
//        findType = getArguments().getString(Constant.FIND_TYPE);
//
////        swipeRefresh = findViewById(R.id.swipe_refresh);
////        swipeRecycler = findViewById(R.id.swipe_recycler);
////
////        SwipeRefreshUtil.setColors(swipeRefresh);
//
////        swipeRecycler.setLayoutManager(new LinearLayoutManager(mActivity));
//        findMsgAdapter = new FindMsgAdapter(mActivity, null);
//        findMsgAdapter.setType(findType);
////        swipeRecycler.setAdapter(findMsgAdapter);
////
////        swipeRefresh.setOnRefreshListener(this);
////        swipeRecycler.setOnLoadMoreListener(this);
//
//        initData();
//    }

//    private void initData() {
//       HttpUtil.getFind(new ProgressSubscriber<FindMsgBean>(mActivity, this) {
//            @Override
//            public void next(FindMsgBean model) {
//                FindMsgBean.DataBean data = model.getData();
//                if (data != null) {
//                    List<FindMsgBean.DataBean.ListBean> list = data.getList();
//                    if (list != null) {
//                        int size = list.size();
//                        if (size > 0) {
//                            if (findMsgAdapter != null) {
//                                if (isRefresh) {
//                                    findMsgAdapter.setData(list);
//                                } else {
//                                    findMsgAdapter.addData(list);
//                                }
//                            }
//                            if (size < NUM) {
//                                setNoMore();
//                            }
//                        } else {
//                            setEmpty();
//                        }
//                    } else {
//                        setEmpty();
//                    }
//                }
//            }
//        }, UserBean.getcToken(mActivity), findType, page, NUM);
//    }

//    private void setEnd() {
//        if (findMsgAdapter != null) {
//            findMsgAdapter.clearData();
//            findMsgAdapter.notifyDataSetChanged();
//            swipeRecycler.setEnd("获取数据为空");
//        }
//    }
//
//    @Override
//    public void showItemFail(String msg) {
//
//    }
//
//    @Override
//    public void hideSwipeLoading() {
//        swipeRefresh.setRefreshing(false);
//    }
//
//    @Override
//    public void setNoMore() {
//        swipeRecycler.setEnd("没有更多数据");
//    }
//
//    @Override
//    public void setEmpty() {
//        swipeRecycler.setEmpty("获取数据为空", R.drawable.empty);
//    }
//
//    @Override
//    public void onRefresh() {
//        isRefresh = true;
//        page = 1;
//        initData();
//    }
//
//    @Override
//    public void onLoadMore() {
//        isRefresh = false;
//        page++;
//        initData();
//    }
//
//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        // TODO: inflate a fragment view
//        View rootView = super.onCreateView(inflater, container, savedInstanceState);
//        unbinder = ButterKnife.bind(this, rootView);
//        return rootView;
//    }
//
//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//        unbinder.unbind();
//    }
}